package com;

import java.sql.SQLOutput;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        System.out.println("项目开始了");
        Scanner input = new Scanner(System.in);
        String [][][] ch = new String[6][7][2];
        initChar(ch);
        System.out.print("请输入要识别的符号串：");
        String str = input.nextLine();
        System.out.println("分析栈             剩余输入串       推导所用的产生式");
        Discern(ch,str);
    }

    public static void initChar(String ch[][][]){  //对预分析表进行存储
        ch[1][0][0]="E";  ch[0][1][0]="i";
        ch[2][0][0]="e"; ch[0][2][0]="+";
        ch[3][0][0]="T";  ch[0][3][0]="*";
        ch[4][0][0]="t"; ch[0][4][0]="(";
        ch[5][0][0]="F";  ch[0][5][0]=")";
                          ch[0][6][0]="#";
        ch[1][1][1]="Te";  ch[1][4][1]="Te";
        ch[2][2][1]="+Te";  ch[2][5][1]="ε"; ch[2][6][1]="ε";
        ch[3][1][1]="Ft";   ch[3][4][1]="Ft";
        ch[4][2][1]="ε";    ch[4][3][1]="*Ft"; ch[4][5][1]="ε"; ch[4][6][1]="ε";
        ch[5][1][1]="i";    ch[5][4][1]="(E)";
    }

    public static void Discern(String ch[][][],String str){ //对输入的符号串进行识别
        Stack<String> stack1 = new Stack<String>();
        Stack<String> stack2 = new Stack<String>();
        for (int i = str.length()-1; i>-1 ; i--) {
            stack2.push(str.charAt(i)+"");
        }
        stack1.push("#");
        stack1.push("E");
        while (!stack2.peek().equals("#") || !stack1.peek().equals("#")){
            String fx = stack1.peek();
            String fh = stack2.peek();
            int x=0;
            int y=0;
            for (int i=1;i<=5;i++){    //分析栈匹配
                if(ch[i][0][0].equals(fx)){
                    x=i;
                    break;
                }
            }
            for(int j=1;j<=6;j++){   //符号栈匹配
                if(ch[0][j][0].equals(fh)){
                    y=j;
                    break;
                }
            }
            if(y==0){
                System.out.println("分析出错！输入的符号串中的字符"+fh+"无法匹配！");
                break;
            }
            String s = ch[x][y][1];
            show(stack1);
            System.out.print("               ");
            show(stack2);
            System.out.print("         "+fx+"->"+s+"\n");

            if(s.equals("ε")){
                stack1.pop();
            }
            else {
                stack1.pop();
                for(int i=s.length()-1;i>-1;i--){
                    stack1.push(s.charAt(i)+"");
                }
                if(stack1.peek().equals(stack2.peek())){
                    System.out.println(stack2.peek()+"匹配");
                    stack1.pop();
                    stack2.pop();
                }
            }
        }
    }

    public static void show(Stack<String> stack){  //栈的反向遍历
        Stack<String> stack1 = new Stack<String>();
        for (String cr : stack) { //入栈
            stack1.push(cr);
        }
        for (String cx : stack1) { //输出
            System.out.print(cx);
        }
    }

}
